package com.example.leetdemo.process;

import java.util.*;

public class Solution729 {

    public static void main(String[] args) {
        MyCalendar myCalendar = new MyCalendar();
        System.err.println(myCalendar.book(10, 20));
        System.err.println(myCalendar.book(15, 25));
        System.err.println(myCalendar.book(20, 30));
    }

    static class MyCalendar {

        SortedMap<Integer, Integer> tree = new TreeMap<>();

        public MyCalendar() {

        }

        public boolean book(int start, int end) {
            SortedMap<Integer, Integer> treeMap = tree.headMap(end);
            if (treeMap.size() == 0) {
                tree.put(start, end);
                return true;
            }
            Integer integer = treeMap.lastKey();
            if (integer == null) {
                tree.put(start, end);
            } else if (integer < start) {
                if (treeMap.get(integer) <= start) {
                    tree.put(start, end);
                } else {
                    return false;
                }
            } else {
                return false;
            }
            return true;
        }
    }

    private boolean binarySearch() {
        return false;
    }

}
